package optimizers.ganeatgeneric.report;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.Map;

import optimizers.commons.GAIndiv;
import optimizers.commons.Logger;
import optimizers.commons.RunConfig;
import optimizers.ganeat.Constants;
import optimizers.ganeatgeneric.Individual;
import optimizers.ganeatgeneric.Species;

public class LoggerNEAT extends Logger{
	private String filename;
	private boolean showInConsole;
	public ArrayList<ArrayList<Species>> speciesVisualData;
	public Map<String, Integer> innovations;
	public Map<String, String> nodeNames;
	public ArrayList<Individual> speciesLib;
	public RunConfig config;
	public StringBuilder builder;
	double[][] speciesData = new double[1][Constants.generations + 1];

	public LoggerNEAT(String filename,boolean showInConsole,
			ArrayList<ArrayList<Species>> speciesVisualData,
			double[][] speciesData, Map<String, Integer> innovations,
			Map<String, String> nodeNames, ArrayList<Individual> speciesLib,
			RunConfig config) {
		super(filename+"TXTVERSION");
		System.out.println(filename);
		this.filename = filename;
		this.showInConsole = showInConsole;
		this.speciesVisualData = speciesVisualData;
		this.speciesData = speciesData;
		this.builder = new StringBuilder();
		this.innovations = innovations;
		this.nodeNames = nodeNames;
		this.speciesLib = speciesLib;
		this.config = config;
	}

	@Override
	public void logToMemory(String message) {
		super.logToMemory(message);
		builder.append(message + "\r\n");
		if (showInConsole) {
			System.out.println(message);
		}
	}

	public void saveToFile() {
		try {
			FileOutputStream saveFile = new FileOutputStream(filename);
			ObjectOutputStream save = new ObjectOutputStream(saveFile);
			save.writeObject(speciesVisualData);
			save.writeObject(speciesData);
			save.writeObject(builder.toString()); //redundant
			save.writeObject(innovations);
			save.writeObject(nodeNames);
			save.writeObject(speciesLib);
			save.writeObject(config);
			save.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
